package org.dusin.wufang.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DatabaseConnection {

    private static Connection conn = null;

    static {
        String DBDRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
        String DBURL = "jdbc:derby:WuFang;create=false";
        String DBUSER = "admin";
        String DBPASSWORD = "admin";
        try {
            Class.forName(DBDRIVER).newInstance();
            System.out.println("Load the embedded driver");
            Properties props = new Properties();
            props.put("user", "admin");
            props.put("password", "admin");
            conn = DriverManager.getConnection(DBURL, props);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private DatabaseConnection() {
    }

    public static Connection getConnection() {
        return conn;
    }

    public static void  close() {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
